# coding: UTF-8
"""
显示当前得分、最高得分、等级和剩余飞船数
"""
import pygame.font
from pygame.sprite import Group

from ship import Ship


class Scoreboard(object):
    """【类】显示得分信息"""
    def __init__(self, FP_settings, FP_screen, FP_stats):
        """初始化显示得分的属性"""
        self.settings = FP_settings
        self.screen = FP_screen
        self.screen_rect = self.screen.get_rect()
        self.stats = FP_stats
        # 显示得分信息时使用的字体设置
        self.text_color = (30, 30, 30)
        self.font = pygame.font.SysFont(None, 48)
        # 准备初始得分图像
        self.prep_now_score()
        self.prep_top_score()
        self.prep_level()
        self.prep_ships()

    def prep_now_score(self) -> None:
        """【类方法】将得分的文字内容转换为图像内容"""
        # score_str = str(self.stats.score)
        rounded_now_score = round(self.stats.score, -1)
        now_score_str = "NOW: " + "{:,}".format(rounded_now_score)
        self.now_score_image = self.font.render(now_score_str, True, self.text_color, self.settings.background_color)
        # 将记分板放在屏幕右上角
        self.now_score_rect = self.now_score_image.get_rect()
        self.now_score_rect.right = self.screen_rect.right - 20     # 类似页边距
        self.now_score_rect.top = 80
        return None

    def prep_top_score(self) -> None:
        """【类方法】最高分数"""
        rounded_top_score = round(self.stats.top_score, -1)
        top_score_str = "TOP: " + "{:,}".format(rounded_top_score)
        self.top_score_image = self.font.render(top_score_str, True, self.text_color, self.settings.background_color)
        # 将最高分放在屏幕中央
        self.top_score_rect = self.top_score_image.get_rect()
        self.top_score_rect.right = self.now_score_rect.right
        self.top_score_rect.top = 20
        return None
    
    def prep_level(self) -> None:
        """【类方法】渲染等级文字"""
        level_str = "LEVEL: " + str(self.stats.level)
        self.level_image = self.font.render(level_str, True, self.text_color, self.settings.background_color)
        # 将等级放在屏幕中央
        self.level_rect = self.level_image.get_rect()
        self.level_rect.centerx = self.screen_rect.centerx
        self.level_rect.top = 20
        return None

    def prep_ships(self) -> None:
        """【类方法】渲染剩余飞船图像"""
        self.ships = Group()
        for LV_ship_number in range(self.stats.ship_available):
            ship = Ship(self.settings, self.screen)
            ship.rect.x = 20 + LV_ship_number * ship.rect.width
            ship.rect.y = 10
            self.ships.add(ship)
        return None

    def show_score(self) -> None:
        """【类方法】在屏幕上显示得分"""
        self.screen.blit(self.now_score_image, self.now_score_rect)
        self.screen.blit(self.top_score_image, self.top_score_rect)
        self.screen.blit(self.level_image, self.level_rect)
        self.ships.draw(self.screen)
        return None
